package com.example.whm.lib;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.holoeverywhere.preference.SharedPreferences;
import org.holoeverywhere.preference.SharedPreferences.Editor;

import com.alibaba.fastjson.JSON;

public class client {

	
	
	DefaultHttpClient client=new DefaultHttpClient();
	
	

	
	
	public  <T> List<T> get(Class<T> t,String url){
		 List<T> list=new ArrayList<T>(); 
		 String result=httpget(url);
		 if (result!=null) {
		   list=JSON.parseArray(result, t);
		}
		return list;
	}
 	public String httpget(String url){
 		  String result = null;  
 	        int statusCode = 0;  
 	        HttpGet getMethod = new HttpGet(url);  
 	       // Log.d(TAG, "do the getRequest,url="+url+"");  
 	        try {  
 	            //getMethod.setHeader("User-Agent", USER_AGENT);
 	  
 	            HttpResponse httpResponse = client.execute(getMethod);  
 	            statusCode = httpResponse.getStatusLine().getStatusCode();  
 	            if (statusCode==200) {
					
 	            	result =EntityUtils.toString(httpResponse.getEntity());
				}
 	            
 	        } catch (Exception e) {  
 	        } finally {  
 	            getMethod.abort();  
 	        }  
 	       String s1= JSON.parseObject(result).getString("users");
 	        return s1;  
 		
 	}
	
	public static <T> List<T> pre_cache_get( SharedPreferences sp,Class<T> t,String key){
		List<T> list=new ArrayList<T>(); 
		//SharedPreferences sp = a.getSharedPreferences("whm",Context.MODE_PRIVATE);  
        String s=sp.getString(key,null);
        if (s!=null) {
        	//JSONArray arr=new JSONArray();
        	list=JSON.parseArray(s, t);
		}
	    return list;  
		
	}
	public static <T> T pre_cache_get2( SharedPreferences sp,Class<T> t,String key){
		T o=null;
		//SharedPreferences sp = a.getSharedPreferences("whm",Context.MODE_PRIVATE);  
		String s=sp.getString(key,null);
		if (s!=null) {
			//JSONArray arr=new JSONArray();
			o=JSON.parseObject(s, t);
		}
		return o;  
		
	}
	
	public static void pre_cache_put( SharedPreferences sp,String key,String o){
		 //SharedPreferences sp = a.getSharedPreferences("whm",Context.MODE_PRIVATE);
         Editor editor = sp.edit();
	     editor.putString(key, o);
	     editor.commit();
	}
	public static void pre_cache_put( SharedPreferences sp,String key,Object o){
		pre_cache_put( sp, key, JSON.toJSONString(o));
	}
 	
 	
 	/*public static void file_cache_put(Activity a,String name,Object o){
 		try {
 			a.getCacheDir().c
			BufferedWriter out = new BufferedWriter(new FileWriter(file), 1024);
			out.write("");
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 	}*/
 	
 	
 	
 	
 	
 	
 	
 	
}
